//
//  ClassifyCollectionViewDelegate.m
//  MyNewProject
//
//  Created by 王春辉 on 12/9/19.
//  Copyright © 2019 sun. All rights reserved.
//

#import "ClassifyCollectionViewDelegate.h"
#import "HomeShopModel.h"

@interface ClassifyCollectionViewDelegate() {
    
}
@property (nonatomic, copy) ClassifyCollectionViewScrollToRowAtIndexPathBlock block;
@property (nonatomic, copy) ClassifyCollectionViewDidSelectItemAtIndexPathBlock selectBlock;
@end

static NSString *const identifier = @"collection_cell_identifier";
static NSString *const headerIdentifier = @"collection_header_identifier";
@implementation ClassifyCollectionViewDelegate

#pragma mark -- mark UICollectionViewDelegate UICollectionViewDataSource

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    ClassifyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    HomeShopClassifyModel *sectionModel = self.dataArray[indexPath.section];
    HomeShopClassifySectionModel *model = sectionModel.goods_type_two[indexPath.item];
    cell.titleLabel.text = model.name;
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:SKIMAGE_NAMED(@"cell_default_img")];
    return cell;
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    HomeShopClassifyModel *sectionModel = self.dataArray[section];
    return sectionModel.goods_type_two.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.dataArray.count;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        // 头
        ClassifyCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerIdentifier forIndexPath:indexPath];
        HomeShopClassifyModel *model = self.dataArray[indexPath.section];
        headerView.titleLabel.text = model.title;
        return headerView;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.selectBlock) {
        self.selectBlock(indexPath); }
}
#pragma mark -- mark UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (!_respondScrll) {
        return; }
    UICollectionView *collectionView = (UICollectionView *)scrollView;
    NSArray <NSIndexPath *>* indexPaths = [collectionView indexPathsForVisibleItems];
    NSIndexPath *firstIndexPath = indexPaths.firstObject;
    for (NSIndexPath *indexPath in indexPaths) {
        if (firstIndexPath.section > indexPath.section) {
            firstIndexPath = indexPath;
        }else if (firstIndexPath.section == indexPath.section) {
            if (firstIndexPath.item > indexPath.item) {
                firstIndexPath = indexPath;
            }
        }else {}
    }
    if (self.block) { self.block(firstIndexPath); }
}

/**
 - (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated; 结束时回调

 @param scrollView scrollView
 */
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y < 0) { return; }
    scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, scrollView.contentOffset.y - 30.5); // 30.5 scrollToItem... 之后collectionView.contentOffset.y 的数值
    _respondScrll = true;
}

#pragma mark -- mark 业务逻辑
/**
 collectionView滑动
 
 @param block block
 */
- (void)scrollViewToRowAtIndexPathBlock:(ClassifyCollectionViewScrollToRowAtIndexPathBlock)block {
    self.block = block;
}
/**
 点击cell回调
 
 @param block block
 */
- (void)didSelectItemAtIndexPathBlock:(ClassifyCollectionViewDidSelectItemAtIndexPathBlock)block {
    self.selectBlock = block;
}
@end
